<?php 
ArrowController::importModel("org.arrowplatform.controls.common.actions.ArrowAbstractUIAction");
class ArrowExecuteAction extends ArrowAbstractUIAction{
    
    private $compiledLink;
    private $compiledCode;
    private $model;
    
    public function configure(){
        parent::configure();
        $this->addProperties(array(
            "confirm" => false,
            "template" => false,
            "model" => "",
            "direct" => 0,
        	"path" => false
        	
        ));
        $this->setDisplay(false);
    }
    
    
    
    public function init(){
    	parent::init();
    	 	
    	$tmpArgs = $this->arguments;
    	
    	if(!$this->getProperty("path"))
        	$tmpArgs["path"] = self::STANDARD_CONTROLER;
        else 
        	$tmpArgs["path"] = $this->getProperty("path");
        if(!isset($tmpArgs["model"]))
        	$tmpArgs["model"] = "#model#";
        if($template = $this->getProperty("template"))
            $tmpArgs["return_tpl"] = $template;
        if(!isset($tmpArgs["action"]))
        	$tmpArgs["action"] = $this->getId();
        if(!isset($tmpArgs["key"]))
        	$tmpArgs["key"] = "#key#";
        
        
        $data = array();
        //todo
        //$this->registerJs( get_class($this), dirname(__FILE__)."/resources/ArrowExecuteAction.js", $this->generateSelector(), $data );
        	
        $code = "";
        if( $confirm = $this->getProperty("confirm") ){
            if($confirm == "1")
                $code.= "if(confirm('Czy napewno chcesz wykonać akcję: ".$this->getName()." ?')){";
            else
                $code.= "if(confirm('".$confirm."')){";
        }
        
        $ajax = $this->getParent("ArrowCtrlAjax");
        
        if( $ajax ){
            $code.= '$.get(\'#href#\', {}, function( json ){ ';
            $children = $ajax->getChildren();
            $success= "function(){ ".ArrowCtrlStateManager::getDefault()->getStateChangeLink( $children[0], array() )." }";
            $code .= ' ArrowResponseReader.read( {response:json, successCallback: '.$success.' } ) ;  ' ;
            $code.= "}, 'json');  ";
            
        }else{
            $code.= 'window.location.href=\'#href#\';';
        }
        if($confirm) $code.= "}";
        
		$this->compiledLink = ArrowTemplateLinker::getDefault()->generateBeanLink( $tmpArgs, true );
		
		if($this->getProperty("direct")){
            $this->compiledCode = '<a '.parent::generateClass().' href="#href#"> '.$this->getName()."</a>";
        }else{
	        $this->compiledCode = '<a '.parent::generateClass().' href="javascript: void(0);" onclick="'.$code.'" rel="'.$code.'"> '.$this->getName()."</a>";
        }
        
        $this->model = $this->getProperty("model");
		
    }
    
    public function generate(  IArrowModel  $context = null ){
    	
    	$tmpArgs = $this->arguments;
    	$tmp = array();    
    	if($context){
			foreach($tmpArgs as &$val){
				if($val{0} == "#")
					$tmp[$val] =  $context[substr($val, 1, -1)];
			}
		}
    	
        if($context){
            $tmp["#key#"] = $context->getPKey();
            $tmp["#model#"] = $context->getModel();
        }else{
            $tmp["#model#"] = $this->getProperty("model");
        }
        
		$tmp["#href#"] =  strtr( $this->compiledLink, $tmp);
		
		return strtr( $this->compiledCode, $tmp );
            
    }
}

?>
